/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.task.controller;

import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.task.entity.BladeJob;
import org.springblade.modules.task.wrapper.JobWrapper;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.task.vo.BladeJobVO;
import org.springblade.modules.task.service.IJobService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 任务表 控制器
 *
 * @author Blade
 * @since 2021-07-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-system/task")
@Api(value = "任务表", tags = "任务表接口")
public class JobController extends BladeController {

    private IJobService jobService;
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入job")
    public R<BladeJobVO> detail(BladeJob bladeJob) {
        BladeJob detail = jobService.getOne(Condition.getQueryWrapper(bladeJob));
        return R.data(JobWrapper.build().entityVO(detail));
    }

    /**
     * 分页 任务表
     */

    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入job")
    public R<IPage<BladeJobVO>> list(BladeJob bladeJob, Query query) {
        IPage<BladeJob> pages = jobService.page(Condition.getPage(query), Condition.getQueryWrapper(bladeJob));
        return R.data(JobWrapper.build().pageVO(pages));
    }


    /**
     * 自定义分页 任务表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入job")
    public R<IPage<BladeJobVO>> page(BladeJobVO job, Query query) {
        IPage<BladeJobVO> pages = jobService.selectJobPage(Condition.getPage(query), job);
        return R.data(pages);
    }

    /**
     * 新增 任务表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入job")
    public R save(@Valid @RequestBody BladeJob bladeJob) {
        return R.status(jobService.save(bladeJob));
    }

    /**
     * 修改 任务表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入job")
    public R update(@Valid @RequestBody BladeJob bladeJob) {
        return R.status(jobService.updateById(bladeJob));
    }

    /**
     * 新增或修改 任务表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入job")
    public R submit(@Valid @RequestBody BladeJob bladeJob) {
        return R.status(jobService.submit(bladeJob));
    }


    /**
     * 删除 任务表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(jobService.remove(ids));
    }


    /**
     * 启动任务
     */
    @PostMapping("/start")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "启动任务", notes = "传入ids")
    public R start(@ApiParam(value = "主键", required = true) @RequestParam String ids) {
        return jobService.start(ids);
    }


    /**
     * 停止任务
     */
    @PostMapping("/stop")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "停止任务", notes = "传入ids")
    public R stop(@ApiParam(value = "主键", required = true) @RequestParam String ids) {
        return jobService.stop(ids);
    }

}
